package com.cloudpolice.door.controller;

import com.cloudpolice.door.feignInterface.EquipmentAdvertisingInterFace;
import com.cloudpolice.door.feignInterface.EquipmentInterFace;
import com.cloudpolice.door.model.EquipmentSiteModel;
import com.google.gson.Gson;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lh.model.ResultVO;
import lh.model.ResultVOPage;
import lh.units.ResultStruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * @author ：梁昊，后端工程师：flyman5，前端工程师：孙耀
 * @create 2020-04-02 12:44
 * @function
 * @editLog
 */
@RestController
@RequestMapping("/equipmentAdvertisingFeignController")
@Api(value = "设备粒子层", description = "设备基本与关联信息方法")
public class EquipmentAdvertisingFeignController {
    @Autowired
    EquipmentAdvertisingInterFace equipmentAdvertisingInterFace;
    @Autowired
    EquipmentInterFace equipmentInterFace;
    @Autowired
    Gson gson;

    /**
     * 增加门禁机广告，方法ID：IN2020040213552868528C3E25B1C17
     *
     * @param id          图片ID（主键）
     * @param communityId 社区ID
     * @param periodId    社区区域ID
     * @param sortNo      出现顺序
     * @param beginDate   发布开始日期
     * @param endDate     发布结束日期
     * @return 广告列表
     */
    @ApiOperation(value = "增加门禁机广告,方法ID：IN2020040213552868528C3E25B1C17", notes = "广告列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "图片ID（主键）", dataType = "String", required = true)
            , @ApiImplicitParam(name = "communityId", value = "社区ID", dataType = "String", required = true)
            , @ApiImplicitParam(name = "periodId", value = "社区区域ID", dataType = "String", required = true)
            , @ApiImplicitParam(name = "sortNo", value = "出现顺序", defaultValue = "0", dataType = "String", required = true)
            , @ApiImplicitParam(name = "beginDate", value = "发布开始日期", dataType = "Date", required = true)
            , @ApiImplicitParam(name = "endDate", value = "发布结束日期", dataType = "Date", required = true)
    })
    @PostMapping("/insertEquipmentAdvertising")
    public ResultVO insertEquipmentAdvertising(@RequestParam(value = "id") String id
            , @RequestParam(value = "communityId") String communityId
            , @RequestParam(value = "periodId") String periodId
            , @RequestParam(value = "sortNo") int sortNo
            , @RequestParam(value = "beginDate") @DateTimeFormat(pattern = "yyyy-MM-dd") Date beginDate
            , @RequestParam(value = "endDate") @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate) {
        if (beginDate.compareTo(endDate) > 0)
            return ResultStruct.error("发布结束日期不能小于发布开始日期", ResultVO.class);
        return equipmentAdvertisingInterFace.insertEquipmentAdvertising(id, communityId, periodId, sortNo, beginDate, endDate);
    }

    /**
     * 修改指定门禁机广告，方法ID：UP2020040213273633191B3C52E2BA1
     *
     * @param id        图片ID（主键）
     * @param sortNo    出现顺序
     * @param beginDate 发布开始日期
     * @param endDate   发布结束日期
     * @return 广告列表
     */
    @ApiOperation(value = "修改指定门禁机广告,方法ID：UP2020040213273633191B3C52E2BA1", notes = "广告列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "图片ID（主键）", dataType = "String", required = true)
            , @ApiImplicitParam(name = "communityId", value = "社区ID", dataType = "String", required = true)
            , @ApiImplicitParam(name = "periodId", value = "社区区域ID", dataType = "String", required = true)
            , @ApiImplicitParam(name = "sortNo", value = "出现顺序", dataType = "String", required = true)
            , @ApiImplicitParam(name = "beginDate", value = "发布开始日期", dataType = "Date", required = true)
            , @ApiImplicitParam(name = "endDate", value = "发布结束日期", dataType = "Date", required = true)
    })
    @PostMapping("/updateEquipmentAdvertisingById")
    public ResultVO updateEquipmentAdvertisingById(@RequestParam(value = "id") String id
            , @RequestParam(value = "communityId") String communityId
            , @RequestParam(value = "periodId") String periodId
            , @RequestParam(value = "sortNo") int sortNo
            , @RequestParam(value = "beginDate") @DateTimeFormat(pattern = "yyyy-MM-dd") Date beginDate
            , @RequestParam(value = "endDate") @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate
    ) {
        if (beginDate.compareTo(endDate) > 0)
            return ResultStruct.error("发布结束日期不能小于发布开始日期", ResultVO.class);

        return equipmentAdvertisingInterFace.updateEquipmentAdvertisingById(id, communityId
                , periodId, sortNo
                , beginDate, endDate);
    }

    /**
     * 删除门禁机广告，方法ID：DE20200402125853418D4A65B5394A1
     *
     * @param id          图片ID（主键）
     * @param communityId 社区ID
     * @param periodId    社区区域ID
     * @return 影响条数
     */
    @ApiOperation(value = "删除门禁机广告,方法ID：DE20200402125853418D4A65B5394A1", notes = "影响条数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "图片ID（主键）", dataType = "String", required = true)
            , @ApiImplicitParam(name = "communityId", value = "社区ID", dataType = "String", required = true)
            , @ApiImplicitParam(name = "periodId", value = "社区区域ID", dataType = "String", required = true)
    })
    @PostMapping("/deleteEquipmentAdvertisingById")
    public ResultVO deleteEquipmentAdvertisingById(@RequestParam(value = "id") String id
            , @RequestParam(value = "communityId") String communityId
            , @RequestParam(value = "periodId") String periodId
    ) {
        return ResultStruct.success(equipmentAdvertisingInterFace.deleteEquipmentAdvertisingById(id, communityId, periodId));
    }

    /**
     * 自动删除门禁机广告，方法ID：DE20200402125853418D4A65B5394A1
     *
     * @return 影响条数
     */
    @ApiOperation(value = "自动删除门禁机广告,方法ID：DE20200402125853418D4A65B5394A1", notes = "影响条数")
    @PostMapping("/deleteEquipmentAdvertisingAuto")
    public ResultVO deleteEquipmentAdvertisingAuto() {
        return ResultStruct.success(equipmentAdvertisingInterFace.deleteEquipmentAdvertisingAuto());
    }

    /**
     * 得到门禁机广告，方法ID：SE20200402124026176C7D091631575
     *
     * @param sn 设备sn
     * @return 广告列表
     */
    @ApiOperation(value = "得到门禁机广告,方法ID：SE20200402124026176C7D091631575", notes = "广告列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "useId", value = "设备SN", dataType = "String", defaultValue = "sn123456", required = true)
    })
    @PostMapping("/selectEquipmentAdvertising")
    public ResultVO selectEquipmentAdvertising(@RequestParam(value = "useId") String sn
    ) {
        String communityId = null;
        String periodId = null;
        final ResultVO resultVOEquipSite = equipmentInterFace.selectEquipmentSiteBySn(sn);
        final List list = gson.fromJson(resultVOEquipSite.getData().toString(), List.class);
        for (Object row :
                list) {
            final EquipmentSiteModel equipmentSiteModel = gson.fromJson(row.toString(), EquipmentSiteModel.class);
            switch (equipmentSiteModel.getRoomType()) {
                case 0:
                    communityId = equipmentSiteModel.getSiteId();//场所
                    break;
                case 1:
                    periodId = equipmentSiteModel.getSiteId();//区域
                    break;
            }
        }
        if (periodId != null && communityId != null)
            return equipmentAdvertisingInterFace.selectEquipmentAdvertising(communityId, periodId);
        else
            return ResultStruct.error(ResultVO.class);
    }

}
